<template>
    <div class='staff'>
        <div class="container">
            <!-- <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" class="search-con">
                <el-form-item label="员工名称" prop="key_name">
                    <el-input v-model="queryParams.key_name" placeholder="请输入" clearable size="small" />
                </el-form-item>
                <el-form-item label="联系方式" prop="key_mobile">
                    <el-input v-model="queryParams.key_mobile" placeholder="请输入" clearable size="small" />
                </el-form-item>
                <el-form-item label="身份证号" prop="key_id_card">
                    <el-input v-model="queryParams.key_id_card" placeholder="请输入" clearable size="small" />
                </el-form-item>
                <el-form-item label="所属部门" prop="key_sector_id">
                    <el-select v-model="queryParams.key_sector_id" placeholder="请输入选择" clearable size="small" @change="getJobsList(queryParams.key_sector_id,1)"
                        filterable>
                        <el-option v-for="dict in depList" :key="dict.id" :label="dict.name" :value="dict.id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="所属岗位" prop="key_jobs_id">
                    <el-select v-model="queryParams.key_jobs_id" placeholder="请输入选择" clearable size="small" filterable>
                        <el-option v-for="dict in jobsList" :key="dict.id" :label="dict.name" :value="dict.id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="状态" prop="key_status">
                    <el-select v-model="queryParams.key_status" placeholder="请选择" clearable size="small">
                        <el-option v-for="dict in statusList" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form> -->
            <el-row :gutter="10" class="mb8">
                <el-col :span="22">
                    <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleShow = 'add';"
                        v-hasPermi="['user:staff:add']">
                        新增</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
            <el-table border v-loading="loading" :data="tableList" ref="tableList" v-tableHeight
                :height="tableList.length > 0 ? '900px' : '120px'">
                <el-table-column label="序号" align="center" prop="id" />
                <el-table-column label="头像" align="center" prop="avatar">
                    <template slot-scope="scope">
                        <div v-if="scope.row.avatar" class="img-list">
                            <el-image style="width: 40px; height: 40px;border:1px solid #eee;border-radius: 50%;"
                                :src="scope.row.avatar" :preview-src-list="[scope.row.avatar]">
                            </el-image>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="姓名" align="center" prop="name" />
                <!-- <el-table-column label="联系方式" align="center" prop="mobile" />
                <el-table-column label="身份证号" align="center" prop="id_card" /> -->
                <el-table-column label="所属部门" align="center" prop="sector_name" />
                <el-table-column label="所属岗位" align="center" prop="jobs_name" />
                <el-table-column label="账号" align="center" prop="login_account" />
                <!-- <el-table-column label="密码" align="center" prop="name" /> -->
                <!-- <el-table-column label="创建人" align="center" prop="name" /> -->
                <el-table-column label="聊天状态" align="center" prop="huanxin_is_open">
                    <template slot-scope="scope">
                        <span>
                            {{ selectDictLabel(huanxinStatus, scope.row.huanxin_is_open) }}
                        </span>
                    </template>
                </el-table-column>
                <el-table-column label="状态" align="center" prop="status">
                    <template slot-scope="scope">
                        <span>
                            {{ selectDictLabel(statusList, scope.row.status) }}
                        </span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)"
                            v-hasPermi="['user:staff:edit']">
                            编辑
                        </el-button>
                        <el-button size="mini" type="text" @click="handleChange(scope.row)"
                            v-hasPermi="['user:staff:edit']">
                            修改密码
                        </el-button>
                        <el-button size="mini" type="text" @click="handlechat(scope.row)" v-hasPermi="['user:staff:del']">
                            开启聊天</el-button>
                        <el-button v-if="scope.row.status == 0" size="mini" type="text" @click="handleAble(scope.row)"
                            v-hasPermi="['user:staff:stop']">停用
                        </el-button>
                        <el-button v-if="scope.row.status == 1" size="mini" type="text" @click="handleAble(scope.row)"
                            v-hasPermi="['user:staff:start']">启用
                        </el-button>
                        <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                            v-hasPermi="['user:staff:del']">
                            删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="pageCount > 0" :pageCount="pageCount" :page.sync="queryParams.page"
                :limit.sync="queryParams.limit" @pagination="getList" />
        </div>
        <el-dialog title="修改密码" visible v-if="handleShow == 'change'" @close="close()" width="500px" append-to-body
            :close-on-click-modal="false">
            <el-form ref="form" :model="form" :rules="rules" label-width="80px" v-loading="formLoading">
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="密码" prop="new_pass">
                            <el-input type="password" style="width:215px;" v-model="form.new_pass" placeholder="请输入密码"
                                show-password />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="close()">取 消</el-button>
                <el-button type="primary" @click="submit" :disabled="formLoading">确 定</el-button>
            </span>
        </el-dialog>
        <add v-if="handleShow == 'edit' || handleShow == 'add'" :data="curRow" :handleShow="handleShow" :isSelf="isSelf"
            :depList="depList" @close="handleShow = ''" @load="getList()" />
    </div>
</template>
<script>
import { getStaffLists, setStaffStatus, delStaff, setStaffLoginPass, setHuanxinChat } from "@/api/user/staff.js"
import { departStatus } from "@/api/dictionary"
import {
    getSectorListsAll, getJobsListsAll
} from "@/api/common"
import add from "./staffAdd.vue"
import throttle from 'lodash.throttle'
import md5 from 'js-md5'
import {
    getObjArr
} from "@/utils/ruoyi.js"
export default {
    name: 'staff',
    props: {

    },
    components: {
        // imgUpdate,
        add
    },
    data() {
        return {
            showSearch: true,
            loading: false,
            pageCount: 0,
            total: 0,
            queryParams: {
                page: 1,
                limit: 20,
                key_name: null,
                key_mobile: null,
                key_id_card: null,
                key_sector_id: null,
                key_jobs_id: null,
                key_status: null,
            },
            tableList: [],
            statusList: departStatus,
            handleShow: "",
            depList: [],
            jobsList: [],
            curRow: {},
            isSelf: false,
            form: {},
            rules: {
                new_pass: [{ required: true, message: '请输入密码', trigger: 'blur' }],
            },
            formLoading: false,
            // 聊天状态
            huanxinStatus: [{
                dictLabel: '已开通',
                dictValue: 1
            },
            {
                dictLabel: '未开通',
                dictValue: 0
            },
            ]
        }
    },
    created() {
        let fun = [
            getSectorListsAll(),
        ]
        Promise.all(fun).then(res => {
            this.depList = res[0].data
            this.getList()
        })
    },
    methods: {
        getList() {
            this.loading = true;
            getStaffLists(this.queryParams).then(res => {
                this.tableList = res.data.lists;
                this.pageCount = res.data.pageCount;
                this.loading = false;
            });
        },
        getJobsList(id) {
            this.$set(this.queryParams, "key_jobs_id", null)
            getJobsListsAll({ sector_id: id }).then(res => {
                this.jobsList = res.data
            })
        },
        handleQuery() {
            this.queryParams.page = 1;
            this.getList();
        },
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        close() {
            this.jobsList = []
            this.form = {}
            this.handleShow = ""
        },
        handleEdit(row) {
            console.log(row, 4444)
            this.curRow = row
            this.handleShow = 'edit'
            // getStaffInfo({ id: row.id }).then(res => {
            //     this.curRow = res.data
            //     this.isSelf = getObjArr("ms_username") == row.login_account ? true : false
            //     this.handleShow = 'edit'
            // })
        },
        handleChange(row) {
            console.log(row, 4444)
            this.form.staff_id = row.id
            this.handleShow = 'change'
        },
        submit: throttle(function () {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    this.formLoading = true;
                    this.form.pass = md5(this.form.new_pass)
                    setStaffLoginPass(this.form).then(res => {
                        this.msgSuccess("修改密码成功！");
                        this.close()
                        this.formLoading = false;
                    }).catch(() => {
                        this.formLoading = false;
                    })
                }
            })
        }, 1000, { trailing: false }),
        handleAble(row) {
            let label = row.status == 0 ? '停用' : '启用';
            this.$confirm(`是否将“${row.name}”${label}?`, "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return setStaffStatus({ staff_id: row.id });
            }).then(() => {
                this.getList();
                this.msgSuccess(`${label}成功`);
            })
        },
        handleDelete(row) {
            this.$confirm('是否确认删除' + '"' + row.name + '"' + '?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return delStaff({ staff_id: row.id });
            }).then(() => {
                this.getList();
                this.msgSuccess("删除成功");
            })
        },
        handlechat(row) {
            this.$confirm('是否确认开启' + '"' + row.name + '"' + '聊天?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return setHuanxinChat({ staff_id: row.id });
            }).then(() => {
                this.getList();
                this.msgSuccess("开启成功");
            })
        },
    }
}
</script>

<style lang='scss' scoped>/deep/.avatar-uploader-icon {
    line-height: 100px;
}</style>
